<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Orders_Detail_Entity extends CI_Model
{
    private $tablename = 'orders_detail';
    private $id = null;
    private $oid = null;
    private $pid = null;
    private $amount = null;
    private $unit = null;
    private $price = null;
    private $total = null;
    private $total_buy = null;
    
    public function setId($Id)
    {
        $this->id = $Id;
    }

    public function setOid($Oid)
    {
        $this->oid = $Oid;
    }

    public function setPid($Pid)
    {
        $this->pid = $Pid;
    }

    public function setAmount($Amount)
    {
        $this->amount = $Amount;
    }

    public function setUnit($Unit)
    {
        $this->unit = $Unit;
    }

    public function setPrice($Price)
    {
        $this->price = $Price;
    }

    public function setTotal($Total)
    {
        $this->total = $Total;
    }

    public function setTotal_buy($Total_buy)
    {
        $this->total_buy = $Total_buy;
    }

    public function checkExist_orders($oid)
    {
        $query = $this->db->get_where('orders', array('id' => $oid));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function checkExist_products($pid)
    {
        $query = $this->db->get_where('products', array('id' => $pid));
        if ($query->num_rows() > 0) {
            return true;
        }
        return false;
    }
    
    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val == null || $val == '') continue;
            $params[$key] = $val;
        }
        
        // foreign key check
        if (!isset($params['oid']) || !$this->checkExist_orders($params['oid'])) {
            return -1;
        }
        // foreign key check
        if (!isset($params['pid']) || !$this->checkExist_products($params['pid'])) {
            return -1;
        }        
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getOrders_DetailById($id)
    {
        return $this->db->get_where($this->tablename, array('id' => $id));
    }
    
    public function getAllList()
    {
        return $this->db->get($this->tablename);
    }
    
    public function TotalOrders_Detail()
    {
        return $this->db->count_all_results('Orders_Detail'); 
    } 
}       
        